import { Component, ViewChild } from '@angular/core';
import { Router } from '@angular/router';
import {
  DataTableComponent,
  DialogService,
  DrawerService,
  IDrawerOpenResult,
  TableWidthConfig,
} from '@devui';
import { JobOrderService } from 'src/app/@core/services/jobOrder/job-order.service';
import { SharedMethodService } from 'src/app/@core/services/Sharedmethod/shared-method.service';
import { AttachmentComponent } from 'src/app/@shared/components/attachment/attachment.component';
import { LogComponent } from 'src/app/@shared/components/log/log.component';
import mockData from 'src/app/@core/data/lrp.json';
import { environment } from 'src/environments/environment';
import { AnalyseComponent } from './analyse/analyse.component';
import { LrpResultComponent } from './lrp-result/lrp-result.component';
@Component({
  selector: 'app-lrp',
  templateUrl: './lrp.component.html',
  styleUrls: ['./lrp.component.scss'],
})
export class LrpComponent {
  msgs: Array<Object> = [];
  rangeDate = [];
  stateOptions = ['已分析', '未分析', '分析中'];
  tableData: any[] = [{}];
  dataTableOptions = {
    columns: [
      {
        field: 'name',
        header: 'LRP单号',
        fieldType: 'text',
        fixedLeft: '300px',
      },
      {
        field: 'name',
        header: '单据类型',
        fieldType: 'text',
      },

      {
        field: 'name',
        header: '状态',
        fieldType: 'text',
      },
      {
        field: 'name',
        header: '备注',
        fieldType: 'text',
      },
      {
        field: 'name',
        header: '日志',
        fieldType: 'text',
      },
      {
        field: 'name',
        header: '创建人',
        fieldType: 'text',
      },
      {
        field: 'name',
        header: '创建时间',
        fieldType: 'text',
      },
      {
        field: 'name',
        header: '审核',
        fieldType: 'text',
        fixedRight: '0px',
      },
    ],
  };
  checkedIds = [];
  pager = {
    total: 20,
    pageIndex: 1,
    pageSize: 20,
  };
  tableWidthConfig: TableWidthConfig[] = [
    {
      field: 'checkbox',
      width: '80px',
    },
    {
      field: '操作',
      width: '220px',
    },
    {
      field: '工单号',
      width: '',
    },
    {
      field: '销售订单号',
      width: '',
    },
    {
      field: '销售订单号',
      width: '',
    },
    {
      field: '产品编码',
      width: '',
    },
    {
      field: '产品名称',
      width: '',
    },
    {
      field: '规格',
      width: '',
    },
    {
      field: '规格',
      width: '',
    },
  ];
  dataTableOptions2 = {
    columns: [
      {
        field: 'code',
        header: '物料编码',
        fieldType: 'text',
      },
      {
        field: 'code',
        header: '图片',
        fieldType: 'text',
      },
      {
        field: 'code',
        header: '物料名称',
        fieldType: 'text',
      },
      {
        field: 'code',
        header: '物料规格',
        fieldType: 'text',
      },
      {
        field: 'code',
        header: '物料描述',
        fieldType: 'text',
      },
      {
        field: 'name',
        header: '物料单位',
        fieldType: 'text',
      },
      {
        field: 'workCode',
        header: '数量',
        fieldType: 'text',
      },
      {
        field: 'workCode',
        header: '来源单号',
        fieldType: 'text',
      },
      {
        field: 'workCode',
        header: '备注',
        fieldType: 'text',
      },
    ],
  }; //表头数据
  tableWidthConfig2: TableWidthConfig[] = [

    {
      field: '物料编码',
      width: '150px',
    },
    {
      field: '图片',
      width: '150px',
    },
    {
      field: '物料名称',
      width: '150px',
    },
    {
      field: '物料规格',
      width: '150px',
    },

    {
      field: '物料描述',
      width: '150px',
    },
    {
      field: '物料单位',
      width: '150px',
    },
    {
      field: '数量',
      width: '150px',
    },
    {
      field: '来源单号',
      width: '150px',
    },
    {
      field: '备注',
      width: '150px',
    },
  ]; //表格宽度配置
  pageSizeOptions = [10, 20, 30, 50, 100];
  partNum: string = null;
  partName: string = null;
  JobOrderNum: string = null;
  JobOrderStatus: string = null;
  JobOrderType: string = null;
  StartDate: string = null;
  EndDate: string = null;
  splitBarSize = '0px';
  partDetail: any = [];

  partDetailHeight = '100%';
  @ViewChild(DataTableComponent, { static: true })
  datatable: DataTableComponent;
  showDetailId: any;
  selectedRowIndex: any;
  constructor(
    private SharedMethodService: SharedMethodService,
    private JobOrderService: JobOrderService,
    private Router: Router,
    private drawerService: DrawerService,
    private dialogService: DialogService
  ) {}
  ngOnInit(): void {
    this.tableData = mockData;
  }
  pageIndexChange($event: any) {} //切换页码
  pageSizeChange($event: any) {} //改变每页显示数量
  showToast(type: any, summary: any, content: any) {
    switch (type) {
      default:
        this.msgs = [{ severity: type, summary: summary, content: content }];
    }
  } //提示框
  rangeDateChange(event: any) {
    this.rangeDate = event;
    this.StartDate = this.rangeDate[0]
      .toLocaleDateString('zh-CN', {
        year: 'numeric',
        month: '2-digit',
        day: '2-digit',
      })
      .replace(/\//g, '/');
    this.EndDate = this.rangeDate[1]
      .toLocaleDateString('zh-CN', {
        year: 'numeric',
        month: '2-digit',
        day: '2-digit',
      })
      .replace(/\//g, '/');
  } //日期范围选择
  cleared() {}
  onRowCheckChange(
    checked: any,
    rowIndex: any,
    nestedIndex: any,
    rowItem: { $checked: any; $halfChecked: boolean }
  ) {
    this.SharedMethodService.onRowCheckChange(
      checked,
      rowIndex,
      nestedIndex,
      rowItem,
      this.datatable
    );
  } //单选
  getCheckedRows() {
    this.SharedMethodService.getCheckedRows(
      this.datatable,
      'jobOrderId'
    ).subscribe((data: any) => {
      this.checkedIds = data;
    });
  } //获取选中行
  search() {}
  search2() {}
  reset() {}
  EditJob(id: any) {
    this.Router.navigate(['/pages/product/edit-lrp/', id]);
  }
  analyse(rowItem: any,dialogtype?:string) {
    const data =  {
      id: rowItem.id,
      analysing: false,
    }
    let text = "开始分析"
    let content = AnalyseComponent;
    if(rowItem.status === '已分析') {
      content = LrpResultComponent;
      text = "结果下达"

    }
    if(rowItem.status === '分析中') {
      data.analysing = true;
      text = "分析中"
    }
    const results = this.dialogService.open({
          id: 'department',
          width: '1200px',
          maxHeight: '800px',
          backdropCloseable: true,
          title: 'LRP分析',
          data: data,
          content: content,
          dialogtype: dialogtype,
          onClose: () => {
          },
          buttons: [
            {
              cssClass: 'stress',
              text: text,
              handler: ($event: Event) => {
                data.analysing = true;
              },
            },
            {
              id: 'btn-cancel',
              cssClass: 'common',
              text: '取消',
              handler: ($event: Event) => {
                results.modalInstance.hide();
              },
            },
          ],
        });
  }
  delete(id: any) {}
  deletes() {}
  Approval(id: any) {}
  unApproval(id: any) {}
  results: IDrawerOpenResult;
  allLog() {}
  GetLogsById(id: any) {}
  onButtonClick(rowItem: any, dialogtype?: string) {}
  getImagesArray(fileURL: string): string[] {
    let urls = fileURL.split(',');
    urls = urls.map((url) => environment.modifiedApiUrl + url);
    return urls;
  }
  showDetail(data: any,rowIndex: any) {
    this.showDetailId = data.id;
    this.partDetail = data.lrpItems;
      this.partDetailHeight = '75%'
      this.splitBarSize = '10px'
      this.selectedRowIndex = rowIndex;
  }
}
